package cn.jiangjiesheng.bootstrap.fileport.demo;

import cn.jiangjiesheng.bootstrap.fileport.interfaces.ExcelTableExporter;
import cn.jiangjiesheng.bootstrap.fileport.interfaces.model.ExcelPictModel;
import cn.jiangjiesheng.bootstrap.fileport.interfaces.model.TableDataModel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * Created by dev@jiangjiesheng.cn on 2021/11/4  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
@SpringBootTest
public class ExcelTableExportExample {

    @Autowired
    private ExcelTableExporter excelTableBuilder;

    @Test
    void exportFile() throws FileNotFoundException {
        String avatar = "https://himg.bdimg.com/sys/portrait/item/c9a3786c6331323334353637383930319c3b.jpg";
        File image = ResourceUtils.getFile("classpath:templates/testdata/img.png");
        // sheet1
        TableDataModel tableDataModel = new TableDataModel();
        tableDataModel.addRow("ID", "姓名", "年龄", "头像","沁园春");
        tableDataModel.addRow("1", "张三", 12, ExcelPictModel.builder().setUrl(avatar).build(),
                ExcelPictModel.builder().setFile(image).build());
        tableDataModel.addRow("2", "李四", 13, ExcelPictModel.builder().setUrl(avatar).build(),
                ExcelPictModel.builder().setFile(image).build());
        tableDataModel.addRow("3", "王五", 16, ExcelPictModel.builder().setUrl(avatar).build(),
                ExcelPictModel.builder().setFile(image).build());
        // sheet2
        TableDataModel tableDataModel2 = new TableDataModel();
        tableDataModel2.addRow("ID", "姓名", "年龄");
        tableDataModel2.addRow("1", "张三", 12);
        tableDataModel2.addRow("2", "李四", 13);
        tableDataModel2.addRow("3", "王五", 16);
        File outputFile = excelTableBuilder
                .addTableDataModel("花名册1", tableDataModel)
                .addTableDataModel("花名册2", tableDataModel2)
                .buildFile();
        System.out.println(outputFile);
    }
}
